<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\Url;
use think\Session;

class Index extends Base
{
	public function index()
	{
		$con = empty(input('param.con')) ? 'index' : input('param.con') ;
		$act = empty(input('param.act')) ? 'main' : input('param.act') ;
		$this->assign('con',$con);		
		$this->assign('act',$act);
		return $this->fetch('Index/index');
	}

	public function main()
	{
		$authmenu = new \think\Authmenu();
		/*where：全部  bo：驳回    shen:待审核 xia：待下载 cai:带财审  luru：待录入*/
		$where['is_del'] = $bo['is_del'] = $shen['is_del'] = $xia['is_del'] = $cai['is_del'] = $luru['is_del'] = 0;
		$uid = Session::get('manage_id');
		$shop_id = DB::name('user')->where('id ='.$uid)->value('shop_id');
		$auth=$authmenu->check($uid,'order_shop');
		if($auth == true){
			$where['shop_id'] = $bo['shop_id'] = $shen['shop_id'] = $shop_id;
		}
		$all = $authmenu->check($uid,'order_person');
		if($all == true){
			$where['user_id'] = $bo['user_id'] = $shen['user_id'] = Session::get('manage_id');
		}
		$bo['status'] = 'reject';
		$shen['status'] = 'confirm';
		$xia['status'] = 'upload';
		$cai['status'] = 'caishen';
		$luru['status'] = 'input'; 
		$allcount = DB::name('order')->where($where)->count();
		$bocount = DB::name('order')->where($bo)->count();
		$shencount = DB::name('order')->where($shen)->count();
		$xiacount = DB::name('order')->where($xia)->count(); 
		$caicount = DB::name('order')->where($cai)->count();
		$lurucount = DB::name('order')->where($luru)->count();

		$this->assign('allcount',$allcount);
		$this->assign('bocount',$bocount);
		$this->assign('shencount',$shencount);
		$this->assign('xiacount',$xiacount);
		$this->assign('caicount',$caicount);
		$this->assign('lurucount',$lurucount);
		/*查询定制订单结束*/
		/*查询成衣订单开始*/
		/*quan ：全部订单  caiwu:财审 reject：驳回 cheng：等待上传压缩包 upload：等待下载 download:待生成条码  */
		$caiwu['status'] = 'caiwu';
		$reject['status'] = 'reject';
		$cheng['status'] = 'cheng';
		$upload['status'] = 'upload';
		$download['status'] = 'download';
		$quan['is_del'] = $caiwu['is_del'] = $reject['is_del'] = $cheng['is_del'] = $upload['is_del'] = $download['is_del'] = 0;
		$clothescount = DB::name('clothes_order')->where($quan)->count();/*全部成衣订单数量*/
		$caiwucount   = DB::name('clothes_order')->where($caiwu)->count();/*成衣待财审订单数量*/
		$rejectcount  = DB::name('clothes_order')->where($reject)->count();/*成衣已驳回订单数量*/
		$chengcount   = DB::name('clothes_order')->where($cheng)->count();/*等待上传压缩包的订单数量*/
		$uploadcount  = DB::name('clothes_order')->where($upload)->count();/*等待下载压缩包的订单数量*/
		$downloadCount= DB::name('clothes_order')->where($download)->count();/*等待生成条形码的订单*/

		/*=============================统计图开始======================================================================*/
		/*KPI的统计开始*/
		$kpiStime = input('param.kpiStime');
		$kpiEtime = input('param.kpiEtime');
		$kpiStime = strtotime($kpiStime);
		$kpiEtime = strtotime($kpiEtime);
		if(!empty($kpiStime) && !empty($kpiEtime))
		{
			$mendian    =   DB::name('order')
			->alias('a')
			->join('shop b','a.shop_id = b.id')
			->join('user c','c.shop_id = b.id')
			->where('a.is_del = 0 and c.group_id in (4,8) and c.is_del = 0 and a.add_time >'.$kpiStime.' and a.add_time <'.$kpiEtime)
			->field('a.shop_id,SUM(a.price) as price,SUM(a.true_price) as true_price, count(a.order_num) as num,b.title,c.truename,count(DISTINCT  (a.cust_id)) as cust_num')
			->group('a.shop_id')
			->select();

		}else{
			$mendian    =   DB::name('order')
			->alias('a')
			->join('shop b','a.shop_id = b.id')
			->join('user c','c.shop_id = b.id')
			->where('a.is_del = 0 and c.group_id in (4,8) and c.is_del = 0')
			->field('a.shop_id,SUM(a.price) as price,SUM(a.true_price) as true_price, count(a.order_num) as num,b.title,c.truename,count(DISTINCT  (a.cust_id)) as cust_num')
			->group('a.shop_id')
			->select();
		}
		foreach ($mendian as $k => $v) {
			/*件单价 = 下单金额 / 下单件数  保留两位有效数字*/
			$mendian[$k]['jian_price'] = number_format($v['price']/$v['num'], 2);
			/*客单价 = 下单金额 / 客户数*/
			$mendian[$k]['cust_price'] = number_format($v['price']/$v['cust_num'], 2);
			/*连带率 = */
			$mendian[$k]['liandai']    = number_format($v['num']/$v['cust_num'],2);
			/*复购业绩  就是重复购买的业绩*/
			if(!empty($kpiStime) && !empty($kpiEtime)){
				$fugou_price = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0  and b.is_New = 0 and  a.shop_id ='.$v['shop_id'].' and a.add_time >'.$kpiStime.' and a.add_time <'.$kpiEtime)->field('SUM(price) as fg_price,count(*) as num,count(DISTINCT  cust_id) as cust_num')->find();
			}else{
				$fugou_price = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0  and b.is_New = 0 and  a.shop_id ='.$v['shop_id'])->field('SUM(price) as fg_price,count(*) as num,count(DISTINCT  cust_id) as cust_num')->find(); 
			}
			$mendian[$k]['fugou_price'] = $fugou_price['fg_price']>0 ?$fugou_price['fg_price'] :0;
			$mendian[$k]['fugou_num']  = $fugou_price['num'];
			$mendian[$k]['fugou_cust']  = $fugou_price['cust_num'];
			if($mendian[$k]['fugou_num']>0 )
			{
				$mendian[$k]['fg_jian_price'] = number_format($mendian[$k]['fugou_price']/$mendian[$k]['fugou_num'], 2);

			}else{
				$mendian[$k]['fg_jian_price'] = 0;
			}
			if($mendian[$k]['fugou_cust']>0)
			{
				$mendian[$k]['fg_cust_price'] = number_format($mendian[$k]['fugou_price']/$mendian[$k]['fugou_cust'], 2);
				$mendian[$k]['fg_liandai']    = number_format($mendian[$k]['fugou_num']/$mendian[$k]['fugou_cust'], 2);
			}else{
				$mendian[$k]['fg_cust_price'] = 0;
				$mendian[$k]['fg_liandai']    = 0;
			}
			/*自建新客户 就是新客户*/
			if(!empty($kpiStime) && !empty($kpiEtime)){
				$ziji_price = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0  and b.is_New = 1 and  a.shop_id ='.$v['shop_id'].' and a.add_time >'.$kpiStime.' and a.add_time <'.$kpiEtime)->field('SUM(price) as zj_price,count(*) as num,count(DISTINCT  cust_id) as cust_num')->find();
			}else{
				$ziji_price = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0  and b.is_New = 1 and  a.shop_id ='.$v['shop_id'])->field('SUM(price) as zj_price,count(*) as num,count(DISTINCT  cust_id) as cust_num')->find(); 
			}
			$mendian[$k]['ziji_price'] = $ziji_price['zj_price'];
			$mendian[$k]['ziji_num'] = $ziji_price['num'];
			$mendian[$k]['ziji_cust']  = $ziji_price['cust_num'];
			if($mendian[$k]['ziji_num'] > 0){
				$mendian[$k]['zj_jian_price'] = number_format($mendian[$k]['ziji_price']/$mendian[$k]['ziji_num'], 2);
			}else{
				$mendian[$k]['ziji_price'] = 0;
				$mendian[$k]['zj_jian_price'] = 0;
			}if($mendian[$k]['ziji_cust'] > 0){
				$mendian[$k]['zj_cust_price'] = number_format($mendian[$k]['ziji_price']/$mendian[$k]['ziji_cust'], 2);
				$mendian[$k]['zj_liandai']    = number_format($mendian[$k]['ziji_num']/$mendian[$k]['ziji_cust'], 2);
			}else{
				$mendian[$k]['zj_cust_price'] = 0;
				$mendian[$k]['zj_liandai']    = 0;
			}
		}
		/*KPI总数统计 全部的数据*/
		if(!empty($kpiStime) && !empty($kpiEtime)){
			$allKPITotal    =   DB::name('order')->field('count(*) as num, SUM(price) as price, SUM(true_price) as true_price,count(DISTINCT  (cust_id)) as cust_num ')->where('is_del = 0 and add_time >'.$kpiStime.' and add_time <'.$kpiEtime)->find();
			$allKPIRepeat   =   DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0  and b.is_New = 0 and a.add_time >'.$kpiStime.' and a.add_time <'.$kpiEtime)->field('SUM(price) as fg_price,count(*) as num,count(DISTINCT  cust_id) as cust_num')->find();
			$allKPINew      =   DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0  and b.is_New = 1  and a.add_time >'.$kpiStime.' and a.add_time <'.$kpiEtime)->field('SUM(price) as zj_price,count(*) as num,count(DISTINCT  cust_id) as cust_num')->find();
		}else{
			$allKPITotal    =   DB::name('order')->field('count(*) as num, SUM(price) as price, SUM(true_price) as true_price,count(DISTINCT  (cust_id)) as cust_num ')->where('is_del = 0')->find();
			$allKPINew      =   DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0  and b.is_New = 1')->field('SUM(price) as zj_price,count(*) as num,count(DISTINCT  cust_id) as cust_num')->find();
			$allKPIRepeat   =   DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0  and b.is_New = 0')->field('SUM(price) as fg_price,count(*) as num,count(DISTINCT  cust_id) as cust_num')->find();
		}
		$allKPI = array();
		$allKPI['all_price']       =   $allKPITotal['price'];
		$allKPI['all_true_price']  =   $allKPITotal['true_price'];
		$allKPI['all_cust']        =   $allKPITotal['cust_num'];
		$allKPI['num']             =   $allKPITotal['num'];
		if($allKPI['num'] > 0){
			$allKPI['all_jiandan']     =   number_format($allKPI['all_price']/$allKPI['num'], 2);/*总的件单价*/
		}else{
			$allKPI['all_jiandan']     = 0;
		}
		if($allKPI['all_cust'] >0){
			$allKPI['all_kedan']       =   number_format($allKPI['all_price']/$allKPI['all_cust'], 2);/*总的客单价*/
			$allKPI['all_liandai']     =   number_format($allKPI['num']/$allKPI['all_cust'], 2);/*总的连带率*/
		}else{
			$allKPI['all_kedan']       = 0;
			$allKPI['all_liandai']     = 0;
		}
		$allKPI['fg_price']        =   $allKPIRepeat['fg_price'];
		$allKPI['fg_num']          =   $allKPIRepeat['num'];
		$allKPI['fg_cust']         =   $allKPIRepeat['cust_num'];
		if($allKPI['fg_num'] > 0){
			$allKPI['fg_jiandan']      =   number_format($allKPI['fg_price']/$allKPI['fg_num'], 2);/*总的复购件单价*/
		}else{
			$allKPI['fg_jiandan']      = 0;
			$allKPI['fg_num']          = 0;
		}
		if($allKPI['fg_cust'] > 0){
			$allKPI['fg_kedan']        =   number_format($allKPI['fg_price']/$allKPI['fg_cust'], 2);/*总的复购客单件*/
			$allKPI['fg_liandai']      =   number_format($allKPI['fg_num']/$allKPI['fg_cust'], 2);/*总的复购连带率*/
		}else{
			$allKPI['fg_kedan']        = 0;
			$allKPI['fg_liandai']      = 0;
			$allKPI['fg_cust']         = 0;
		}
		$allKPI['new_price']       =   $allKPINew['zj_price'];
		$allKPI['new_num']         =   $allKPINew['num'];
		$allKPI['new_cust']        =   $allKPINew['cust_num'];
		if($allKPI['new_num'] > 0){
			$allKPI['new_jiandan']     =   number_format($allKPI['new_price']/$allKPI['new_num'], 2);/*总的新建件单价*/
		}else{
			$allKPI['new_jiandan']     = 0;
			$allKPI['new_num']         = 0;
		}
		if($allKPI['new_cust'] > 0){
			$allKPI['new_kedan']       =   number_format($allKPI['new_price']/$allKPI['new_cust'], 2);/*总的客单件*/
			$allKPI['new_liandai']     =   number_format($allKPI['new_num']/$allKPI['new_cust'], 2);/*总的连带率*/
		}else{
			$allKPI['new_kedan']       = 0;
			$allKPI['new_liandai']     = 0;
			$allKPI['new_cust']        = 0;
		}
		/*KPI统计结束*/
		/*==============================================================================================================*/
		/*三大门店业绩指标*/
		/*获取到本月第一天的时间戳*/
        $beginThismonth=mktime(0,0,0,date('m'),1,date('Y'));//月初第一天时间戳
        $yeji = DB::name('order')
        ->alias('a')
        ->join('shop b','b.id = a.shop_id')
        ->field("DATE_FORMAT(FROM_UNIXTIME(a.add_time,'%Y-%m-%d %H:%i:%s'), '%m-%d') AS days,count(*),a.shop_id,SUM(a.price) as price,b.title,SUM(true_price) as true_price")
        ->where('a.is_del = 0  and a.add_time >'.$beginThismonth)
        ->group('shop_id')
        ->select(); 
        foreach ($yeji as $k => $v) {
        	$v['yeji'] = DB::name('shop_yeji')->where('is_del = 0 and shopid ='.$v['shop_id'].' and month ='.$beginThismonth)->value('money');
        	$chu = DB::name('shop_clothes_row')->field("DATE_FORMAT(FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') AS days, SUM(price) as price,SUM(sale_price) as sale_price")
        	->where('is_del = 0  and addtime >'.$beginThismonth.' and shopid = '.$v['shop_id'])
        	->group('shopid')
        	->find();
        	$v['yeji'] = $v['yeji'] > 0 ? $v['yeji'] : 0 ;
        	if($v['yeji']>0){
        		$dabiao[] = number_format((($v['true_price']+$chu['sale_price'])/$v['yeji'])*100, 2);
        	}else{
        		$dabiao[] = 0;
        	}
        	$yj[] = $v['yeji'];
        	$shop_name[] = $v['title'];
        	$shop_price[] = $v['price']+$chu['price'];
        	$shop_true_price[] = $v['true_price']+$chu['sale_price'];

        }
        if(count($yeji)>0){
        	$shop_name = json_encode($shop_name);
        }else{
        	$array = array('西安西大街店','西安龙首店','磐安体验店');
        	$yj = array(0,0,0);
        	$shop_price = array(0,0,0);
        	$dabiao = array(0,0,0);
        	$shop_true_price = array(0,0,0);
        	$shop_name = json_encode($array);
        }
        $shop_price = json_encode($shop_price);
        $shop_yj = json_encode($yj);
        $dabiao = json_encode($dabiao);
        $shop_true_price = json_encode($shop_true_price);
        /*=========================================三大门店业绩指标结束==================================*/
        /*三大门店每日业绩显示*/
            $yue = mktime(0,0,0,date('m'),1,date('Y'));//月初第一天时间戳
            $daySum =  date("t",strtotime(date("Y-m",time())));//获取本月的天数
            $ereryday   = DB::name('order')
            ->field("DATE_FORMAT(FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s'), '%m-%d') AS days,count(*),shop_id,SUM(true_price) as price")
            ->where('is_del = 0  and add_time >'.$yue)
            ->group("`shop_id`,DATE_FORMAT(FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s'), '%Y-%m-%d')")
            ->order('days')
            ->select();
        //var_dump(DB::name('order')->getLastSql()).die();
            $shopOneAvg = array();
            $shopTwoAvg = array();
            $shopFiveAvg = array();     
            foreach ($ereryday as $k => $v) {
            	$Onenum = DB::name('shop_yeji')->where('is_del =0 and shopid = 1 and month = '.$yue)->value('money');
            	if($Onenum > 0){
            		$Oneavg = $Onenum/$daySum;
            		$shopOne_avg = round($Oneavg, 2);
            	}else{
            		$shopOne_avg = 0;
            	}
            	$Twonum = DB::name('shop_yeji')->where('is_del =0 and shopid = 2 and month = '.$yue)->value('money');
            	if($Twonum > 0){
            		$shopTwo_avg = $Twonum/$daySum;
            		$shopTwo_avg = round($shopTwo_avg ,2);
            	}else{
            		$shopTwo_avg = 0;                       
            	}
            	$fivenum = DB::name('shop_yeji')->where('is_del =0 and shopid = 5 and month = '.$yue)->value('money');
            	if($fivenum >0){
            		$shopFive_avg = $fivenum/$daySum;
            		$shopFive_avg = round($shopFive_avg,2);
            	}else{
            		$shopFive_avg = 0;
            	}

            	switch ($v['shop_id']) {
            		case '1':
            		$chu = DB::name('shop_clothes_row')->field('SUM(sale_price) as sale_price')->where("is_del = 0 and shopid = 1 and DATE_FORMAT(FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') = '".$v['days']."'")->find();
            		$v['price'] = $v['price']+$chu['sale_price'];
            		$shop_one[$v['days']] = $v['price']>0 ?$v['price'] : 0;
            		for ($i=0; $i < $daySum; $i++) { 
            			array_push($shopOneAvg, $shopOne_avg);
            		}
            		break;
            		case '2':
            		$chu = DB::name('shop_clothes_row')->field('SUM(sale_price) as sale_price')->where("is_del = 0 and shopid = 2 and DATE_FORMAT(FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') = '".$v['days']."'")->find();
            		$v['price'] = $v['price']+$chu['sale_price'];
            		$shop_two[$v['days']] = $v['price']>0 ?$v['price'] : 0;
            		for ($i=0; $i < $daySum; $i++) {
            			array_push($shopTwoAvg, $shopTwo_avg);
            		}
            		break;
            		case '5':
            		$chu = DB::name('shop_clothes_row')->field('SUM(sale_price) as sale_price')->where("is_del = 0 and shopid = 5 and DATE_FORMAT(FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') = '".$v['days']."'")->find();
            		$v['price'] = $v['price']+$chu['sale_price'];
            		$shop_five[$v['days']] = $v['price']>0 ?$v['price'] : 0;
            		for ($i=0; $i < $daySum; $i++) { 
            			array_push($shopFiveAvg, $shopFive_avg);
            		}
            		break;
            	}
            }
            /*判断数组中是都存在店铺id*/
            $first_isnull['shop_id']	=	1;
            $two_isnull['shop_id']	=	2;
            $five_isnull['shop_id']	=	5;
            for($i=0;$i<$daySum;$i++){
                     $array[date('m-d',$yue+$i*86400)] =   0; //每隔一天赋值给数组
                     $date[] = date('m-d',$yue+$i*86400);
                     if(count($ereryday)==0){
                     	$yi = DB::name('shop_clothes_row')->field('SUM(sale_price) as sale_price')->where("is_del = 0 and shopid = 1 and DATE_FORMAT(FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') = '".date('m-d',$yue+$i*86400)."'")->find();
                     	$er = DB::name('shop_clothes_row')->field('SUM(sale_price) as sale_price')->where("is_del = 0 and shopid = 2 and DATE_FORMAT(FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') = '".date('m-d',$yue+$i*86400)."'")->find();
                     	$wu = DB::name('shop_clothes_row')->field('SUM(sale_price) as sale_price')->where("is_del = 0 and shopid = 5 and DATE_FORMAT(FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') = '".date('m-d',$yue+$i*86400)."'")->find();
                     	$shop_one[date('m-d',$yue+$i*86400)] = 0+$yi['sale_price'];
                     	$shop_two[date('m-d',$yue+$i*86400)] = 0+$er['sale_price'];
                     	$shop_five[date('m-d',$yue+$i*86400)]= 0+$wu['sale_price'];
                     	array_push($shopOneAvg, 0);
                     	array_push($shopTwoAvg, 0);
                     	array_push($shopFiveAvg, 0);
                     }else{
                     	if(deep_in_array('1', $ereryday) != true){
                     		$yi = DB::name('shop_clothes_row')->field('SUM(sale_price) as sale_price')->where("is_del = 0 and shopid = 1 and DATE_FORMAT(FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') = '".date('m-d',$yue+$i*86400)."'")->find();
                     		$shop_one[date('m-d',$yue+$i*86400)] = 0+$yi['sale_price'];
                     	}if(deep_in_array('2', $ereryday) != true){
                     		$er = DB::name('shop_clothes_row')->field('SUM(sale_price) as sale_price')->where("is_del = 0 and shopid = 2 and DATE_FORMAT(FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') = '".date('m-d',$yue+$i*86400)."'")->find();
                     		$shop_two[date('m-d',$yue+$i*86400)] = 0+$er['sale_price'];
                     	}if(deep_in_array('5', $ereryday) != true){
                     		$wu = DB::name('shop_clothes_row')->field('SUM(sale_price) as sale_price')->where("is_del = 0 and shopid = 5 and DATE_FORMAT(FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i:%s'), '%m-%d') = '".date('m-d',$yue+$i*86400)."'")->find();
                     		$shop_five[date('m-d',$yue+$i*86400)]= 0+$wu['sale_price'];
                     	}
                     }
                 }
                 $a =  array_merge($array, $shop_one);
                 $b =  array_merge($array, $shop_two);
                 $c =  array_merge($array, $shop_five);
                 $shop_one = json_encode(array_values($a)) ;
                 $shop_two = json_encode(array_values($b)) ;
                 $shop_five= json_encode(array_values($c)) ;
                 $shopOne_avg = json_encode($shopOneAvg);
                 $shopTwo_avg = json_encode($shopTwoAvg);
                 $shopFive_avg = json_encode($shopFiveAvg);
                 $date = json_encode($date);
                 /*每日销售业绩显示*/
                 /*==================================每日销售业绩显示展示完毕==============================================================*/
                 /*本月各店业绩占比*/
                 $benYue =   $this->yejiZhanbi();
                 $menDian = $this->yejiDianpu();
                 /*==================================本月各店业绩占比展示完毕==============================================================*/
                 /*品类占比分析*/
                 $categoryPercent   =   $this->categoryPercent();
                 $categoryName      =   $this->categoryName();

                 $getOrderAge    =   $this->getOrderAge();


                 $getOnlineNum    =   $this->getOnlineNum();

                 $getOrderPrice   =   $this->getOrderPrice();

                 $getOrderGongchang  =   $this->getOrderGongchang();

                 $this->assign('getOrderGongchang', $getOrderGongchang);
                 $this->assign('clothescount',$clothescount);
                 $this->assign('caiwucount',$caiwucount);
                 $this->assign('rejectcount',$rejectcount);
                 $this->assign('chengcount',$chengcount);
                 $this->assign('uploadcount',$uploadcount);
                 $this->assign('downloadCount',$downloadCount);
                 $this->assign('mendian',$mendian);
                 $this->assign('shopName',$shop_name);
                 $this->assign('shopPrice',$shop_price);
                 $this->assign('shopYeji',$shop_yj);
                 $this->assign('shopDabiao',$dabiao);
                 $this->assign('shopTruePrice',$shop_true_price);
                 $this->assign('shopOne',$shop_one);
                 $this->assign('shopTwo',$shop_two);
                 $this->assign('shopFive',$shop_five);
                 $this->assign('shopAvgOne',$shopOne_avg);    
                 $this->assign('shopAvgTwo',$shopTwo_avg);    
                 $this->assign('shopAvgFive',$shopFive_avg); 
                 $this->assign('date' ,$date);
                 $this->assign('benyue', $benYue);
                 $this->assign('dianpu', $menDian);
                 $this->assign('categoryPercent',$categoryPercent);
                 $this->assign('categoryName', $categoryName);
                 $this->assign('getOrderAge', $getOrderAge);
                 $this->assign('getOnlineNum', $getOnlineNum);
                 $this->assign('getOrderPrice', $getOrderPrice);
                 $this->assign('allKPI', $allKPI);
                 return $this->fetch('Index/main');
             }

             private  function  yejiZhanbi()
             {
        $start_time = strtotime(date('Y-m-01'));  //获取本月第一天时间戳
        //$start_time = 1533081600;
        $allNUM  = DB::name('order')->where('is_del = 0 and add_time >'.$start_time)->value('SUM(price)');
        $all_price  = DB::name('order')
        ->alias('a')
        ->join('shop b','a.shop_id = b.id')
        ->field('b.title,a.shop_id,SUM(a.true_price) as price')
        ->where('a.is_del = 0 and a.add_time >'.$start_time)
        ->group('a.shop_id')
        ->select();
        $arr = array();
        foreach($all_price as $k=>$v){
        	$chu = DB::name('shop_clothes_row')->field('SUM(sale_price) as sale_price')->where('is_del = 0 and shopid = '.$v['shop_id'].' and addtime >'.$start_time)->find();
        	$array['value'] = $v['price']+$chu['sale_price'];
        	$array['name'] =   $v['title'];
        	$arr1[] = $v['title'];
        	$arr[]=(object)$array;
        }
        return json_encode($arr);
    }
    private  function  yejiDianpu()
    {
        $start_time = strtotime(date('Y-m-01'));  //获取本月第一天时间戳
        //$start_time = 1533081600;
        $all_price  = DB::name('order')
        ->alias('a')
        ->join('shop b','a.shop_id = b.id')
        ->field('b.title,a.shop_id,SUM(a.true_price) as price')
        ->where('a.is_del = 0 and a.add_time >'.$start_time)
        ->group('a.shop_id')
        ->select();
        foreach($all_price as $k=>$v){
        	$array[]    =   $v['title'];
        }
        return json_encode($array);
    }
    /*品类占比分析*/
    private function categoryPercent($stime = null, $etime = null)
    {
    	$stime = strtotime($stime);
    	$etime = strtotime($etime);
    	if(!empty($stime) && !empty($etime))
    	{
    		$where = 'a.is_del = 0 and a.add_time > '.$stime.' and a.add_time < '.$etime;
    	}else{
    		$where = 'a.is_del = 0';
    	}
    	$list = DB::name('order')
    	->alias('a')
    	->join('category b','b.id = a.category_id')
    	->field('b.title,count(a.order_num) as num')
    	->where($where)
    	->group('a.category_id')
    	->select();
    	$arr = array();
    	if(count($list) > 0){
    		foreach($list as $k=>$v){
    			$array['value'] = $v['num'];
    			$array['name'] = $v['title'];
    			$arr[] = (object)$array;
    		}
    	}else{
    		$array['value'] = 0;
    		$array['name'] = '暂无分类';
    		$arr[] = (object)$array;
    	}
    	return json_encode($arr);
    }
    /*获取所有订单中的品类*/
    private  function  categoryName($stime = null, $etime = null)
    {
    	$stime = strtotime($stime);
    	$etime = strtotime($etime);
    	if(!empty($stime) && !empty($etime))
    	{
    		$where = 'a.is_del = 0 and a.add_time > '.$stime.' and a.add_time < '.$etime;
    	}else{
    		$where = 'a.is_del = 0';
    	}
    	$list = DB::name('order')
    	->alias('a')
    	->join('category b','b.id = a.category_id')
    	->field('b.title,count(a.order_num) as num')
    	->where($where)
    	->group('a.category_id')
    	->select();
    	if(count($list) > 0)
    	{
    		foreach($list as $k=>$v)
    		{
    			$arr[] = $v['title'];
    		}
    	}else{
    		$arr[] = '暂无分类';
    	}
    	return json_encode($arr);
    }
    /*获取所有订单中的年龄区间*/
    private  function  getOrderAge($stime = null, $etime = null)
    {
    	$stime = strtotime($stime);
    	$etime = strtotime($etime);
    	$arr = array();
    	if(!empty($stime) && !empty($etime))
    	{
    		/*计算年龄在20一下*/
    		$num1 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age <20 and a.add_time > '.$stime.' and a.add_time < '.$etime)->count();
    		$stemp['value'] = $num1;
    		$stemp['name']  = '20岁以下';
    		$arr[]=(object)$stemp;
    		/*计算年龄在20-25*/
    		$num2 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=20 and b.age < 25 and a.add_time > '.$stime.' and a.add_time < '.$etime)->count();
    		$stemp['value'] = $num2;
    		$stemp['name']  = '20-25岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在25-30*/
    		$num3 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=25 and b.age < 30 and a.add_time > '.$stime.' and a.add_time < '.$etime)->count();
    		$stemp['value'] = $num3;
    		$stemp['name']  = '25-30岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在30-35*/
    		$num4 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=30 and b.age < 35 and a.add_time > '.$stime.' and a.add_time < '.$etime)->count();
    		$stemp['value'] = $num4;
    		$stemp['name']  = '30-35岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在35-40*/
    		$num5 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=35 and b.age < 40 and a.add_time > '.$stime.' and a.add_time < '.$etime)->count();
    		$stemp['value'] = $num5;
    		$stemp['name']  = '35-40岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在40-45*/
    		$num6 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=40 and b.age < 45 and a.add_time > '.$stime.' and a.add_time < '.$etime)->count();
    		$stemp['value'] = $num6;
    		$stemp['name']  = '40-45岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在45-50*/
    		$num7 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=45 and b.age < 50 and a.add_time > '.$stime.' and a.add_time < '.$etime)->count();
    		$stemp['value'] = $num7;
    		$stemp['name']  = '45-50岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在50-55*/
    		$num8 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=50  and a.add_time > '.$stime.' and a.add_time < '.$etime)->count();
    		$stemp['value'] = $num8;
    		$stemp['name']  = '50岁以上';
    		$arr[]=(object)$stemp;
    	}else{
    		/*计算年龄在20一下*/
    		$num1 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age <20')->count();
    		$stemp['value'] = $num1;
    		$stemp['name']  = '20岁以下';
    		$arr[]=(object)$stemp;
    		/*计算年龄在20-25*/
    		$num2 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=20 and b.age < 25')->count();
    		$stemp['value'] = $num2;
    		$stemp['name']  = '20-25岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在25-30*/
    		$num3 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=25 and b.age < 30')->count();
    		$stemp['value'] = $num3;
    		$stemp['name']  = '25-30岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在30-35*/
    		$num4 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=30 and b.age < 35')->count();
    		$stemp['value'] = $num4;
    		$stemp['name']  = '30-35岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在35-40*/
    		$num5 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=35 and b.age < 40')->count();
    		$stemp['value'] = $num5;
    		$stemp['name']  = '35-40岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在40-45*/
    		$num6 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=40 and b.age < 45')->count();
    		$stemp['value'] = $num6;
    		$stemp['name']  = '40-45岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在45-50*/
    		$num7 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=45 and b.age < 50')->count();
    		$stemp['value'] = $num7;
    		$stemp['name']  = '45-50岁';
    		$arr[]=(object)$stemp;
    		/*计算年龄在50以上*/
    		$num8 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del = 0 and b.age >=50')->count();
    		$stemp['value'] = $num8;
    		$stemp['name']  = '50岁以上';
    		$arr[]=(object)$stemp;
    	}
    	return json_encode($arr);
    }
    /*线上线下订单数量占比*/
    private function getOnlineNum($stime = null, $etime = null)
    {
    	$stime = strtotime($stime);
    	$etime = strtotime($etime);
    	$arr = array();
    	if(!empty($stime) && !empty($etime))
    	{
    		$num1 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del =0 and b.from =3 and a.add_time > '.$stime.' and a.add_time < '.$etime)->count();
    		$stemp['value'] = $num1;
    		$stemp['name'] = '线上订单数';
    		$arr[] = (object)$stemp;
    		$num2 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del =0 and b.from <>3 and a.add_time > '.$stime.' and a.add_time < '.$etime)->count();
    		$stemp['value'] = $num2;
    		$stemp['name'] = '线下订单数';
    		$arr[] = (object)$stemp;
    	}else{
    		$num1 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del =0 and b.from =3')->count();
    		$stemp['value'] = $num1;
    		$stemp['name'] = '线上订单数';
    		$arr[] = (object)$stemp;
    		$num2 = DB::name('order')->alias('a')->join('customer b','a.cust_id = b.id')->where('a.is_del =0 and b.from <>3')->count();
    		$stemp['value'] = $num2;
    		$stemp['name'] = '线下订单数';
    		$arr[] = (object)$stemp;
    	}
    	return json_encode($arr);
    }

    /*消费金额占比分析*/
    private function  getOrderPrice($stime = null, $etime = null)
    {
    	$stime = strtotime($stime);
    	$etime = strtotime($etime);
    	$arr    =   array();
    	/*计算500块钱以下的*/
    	if(!empty($stime) && !empty($etime))
    	{
    		$num1   =   DB::name('order')->where('is_del = 0 and true_price <500 and add_time >'.$stime.' and add_time <'.$etime)->count();
    		$stemp['value'] = $num1;
    		$stemp['name'] = '0.5K以下';
    		$arr[] = (object)$stemp;
    		$num3   =   DB::name('order')->where('is_del = 0 and true_price >=500 and true_price <1000 and add_time >'.$stime.' and add_time <'.$etime)->count();
    		$stemp['value'] = $num3;
    		$stemp['name'] = '0.5-1K';
    		$arr[] = (object)$stemp;
    		$num5   =   DB::name('order')->where('is_del = 0 and true_price >=1000 and true_price <1500 and add_time >'.$stime.' and add_time <'.$etime)->count();
    		$stemp['value'] = $num5;
    		$stemp['name'] = '1-1.5K';
    		$arr[] = (object)$stemp;
    		/*计算1750-2000块钱*/
    		$num7   =   DB::name('order')->where('is_del = 0 and true_price >=1500 and true_price <2000 and add_time >'.$stime.' and add_time <'.$etime)->count();
    		$stemp['value'] = $num7;
    		$stemp['name'] = '1.5-2K';
    		$arr[] = (object)$stemp;
    		/*计算2250-2500块钱*/
    		$num9   =   DB::name('order')->where('is_del = 0 and true_price >=2000 and true_price <2500 and add_time >'.$stime.' and add_time <'.$etime)->count();
    		$stemp['value'] = $num9;
    		$stemp['name'] = '2-2.5K';
    		$arr[] = (object)$stemp;
    		/*计算2750-3000块钱*/
    		$num11   =   DB::name('order')->where('is_del = 0 and true_price >=25.0 and true_price <3000 and add_time >'.$stime.' and add_time <'.$etime)->count();
    		$stemp['value'] = $num11;
    		$stemp['name'] = '2.5-3K';
    		$arr[] = (object)$stemp;
    		/*计算3000块钱以上*/
    		$num12   =   DB::name('order')->where('is_del = 0 and true_price >=3000 and add_time >'.$stime.' and add_time <'.$etime)->count();
    		$stemp['value'] = $num12;
    		$stemp['name'] = '3K以上';
    		$arr[] = (object)$stemp;
    	}else{
    		$num1   =   DB::name('order')->where('is_del = 0 and true_price <500')->count();
    		$stemp['value'] = $num1;
    		$stemp['name'] = '0.5K以下';
    		$arr[] = (object)$stemp;
    		/*计算750-1000块钱*/
    		$num3   =   DB::name('order')->where('is_del = 0 and true_price >=500 and true_price <1000')->count();
    		$stemp['value'] = $num3;
    		$stemp['name'] = '0.5-1K';
    		$arr[] = (object)$stemp;
    		/*计算1250-1500块钱*/
    		$num5   =   DB::name('order')->where('is_del = 0 and true_price >=1000 and true_price <1500')->count();
    		$stemp['value'] = $num5;
    		$stemp['name'] = '1-1.5K';
    		$arr[] = (object)$stemp;
    		/*计算1500-1750块钱*/
    		$num7   =   DB::name('order')->where('is_del = 0 and true_price >=1500 and true_price <2000')->count();
    		$stemp['value'] = $num7;
    		$stemp['name'] = '1.5-2K';
    		$arr[] = (object)$stemp;
    		/*计算2250-2500块钱*/
    		$num9   =   DB::name('order')->where('is_del = 0 and true_price >=2000 and true_price <2500')->count();
    		$stemp['value'] = $num9;
    		$stemp['name'] = '2-2.5K';
    		$arr[] = (object)$stemp;
    		/*计算2750-3000块钱*/
    		$num11   =   DB::name('order')->where('is_del = 0 and true_price >=2500 and true_price <3000')->count();
    		$stemp['value'] = $num11;
    		$stemp['name'] = '2.5-3K';
    		$arr[] = (object)$stemp;
    		/*计算3000块钱以上*/
    		$num12   =   DB::name('order')->where('is_del = 0 and true_price >=3000')->count();
    		$stemp['value'] = $num12;
    		$stemp['name'] = '3K以上';
    		$arr[] = (object)$stemp;
    	}
    	return json_encode($arr);
    }
    /*工厂订单占比分析的方法*/
    private  function getOrderGongchang($stime = null, $etime = null)
    {
    	$stime = strtotime($stime);
    	$etime = strtotime($etime);
    	$arr    =   array();
    	/*计算500块钱以下的*/
    	if(!empty($stime) && !empty($etime))
    	{
    		$where = 'is_del = 0 and add_time > '.$stime.' and add_time < '.$etime;
    	}else{
    		$where = 'is_del = 0';
    	}
    	$data = DB::name('order')->where($where)->field('gongchang,count(order_num) as num')->group('gongchang')->order('gongchang')->select();
    	foreach($data as $k=>$v){
    		switch($v['gongchang'])
    		{
    			case '-1':
    			$stemp['value']   =   $v['num'];
    			$stemp['name']    =   '翔宇丹工厂';
    			$arr[] = (object)$stemp;
    			break;
    			case '1':
    			$stemp['value']   =   $v['num'];
    			$stemp['name']    =   '红岭工厂';
    			$arr[] = (object)$stemp;
    			break;
    			case '2':
    			$stemp['value']   =   $v['num'];
    			$stemp['name']    =   '如意工厂';
    			$arr[] = (object)$stemp;
    			break;
    			case '3':
    			$stemp['value']   =   $v['num'];
    			$stemp['name']    =   '鲁泰工厂';
    			$arr[] = (object)$stemp;
    			break;
    		}
    	}
    	return json_encode($arr);
    }
    /*利用ajax来进行数据的按时间筛选*/
    public  function getDataForAjax()
    {
    	if(request::instance()->isPost())
    	{
    		$stime = request::instance()->post('stime');
    		$etime = request::instance()->post('etime');
    		$data['getOrderPrice']   = $this->getOrderPrice($stime, $etime);
    		$data['categoryPercent'] = $this->categoryPercent($stime, $etime); 
    		$data['categoryName']    = $this->categoryName($stime, $etime);
    		$data['getOrderAge']     = $this->getOrderAge($stime, $etime);
    		$data['getOnlineNum']    = $this->getOnlineNum($stime, $etime);
    		$data['getOrderGongchang'] = $this->getOrderGongchang($stime, $etime);
    		return $data;
    	}
    }
}
